﻿using MES.Desktop.Common;
using MES.Desktop.IService;
using MES.Desktop.SystemModule.Models;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MES.Desktop.SystemModule.ViewModels.Dialogs
{
    public class ModifyFuncViewModel : DialogViewModelBase
    {
        public FuncModel FuncInfo { get; set; } =
                new FuncModel();


        IFuncService _funcService;
        public ModifyFuncViewModel(IFuncService funcService)
        {
            _funcService = funcService;
            FuncInfo = new FuncModel(_funcService);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            var model = parameters.GetValue<FuncModel>("model");

            if (model == null)
            {
                Title = "新增功能名称";
            }
            else
            {
                Title = "编辑功能名称";

                FuncInfo.FuncId = model.FuncId;
                FuncInfo.FuncName = model.FuncName;
            }
        }

        public override void DoSave()
        {
            if (FuncInfo.HasErrors) return;
            try
            {
                Entities.FuncEntity funcEntity = new Entities.FuncEntity
                {
                    FuncId = FuncInfo.FuncId,
                    FuncName = FuncInfo.FuncName
                };

                _funcService.Update(funcEntity);

                base.DoSave();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
